<template>
    <div>
        <!--顶部输入框-->
        <div>
            <el-input
                    size="small"
                    class="addPosInput"
                    placeholder="添加职位..."
                    suffix-icon="el-icon-plus"
                    v-model="pos.name"
                    @keydown.enter.native="addPosition"
            ></el-input>
            <el-button
                    type="primary"
                    icon="el-icon-plus"
                    size="small"
                    @click="addPosition"
            >添加
            </el-button>
        </div>
        <!--表格部分-->
        <div>
            <el-table class="PositionsTable"
                      :data="Positions"
                      stripe
                      @selection-change="handleSelectionChange"
                      style="width: 65%">
                <el-table-column
                        type="selection"
                        width="60">
                </el-table-column>
                <el-table-column
                        label="索引"
                        type="index"
                        width="50"
                        :index="indexMethod">
                </el-table-column>
                <el-table-column
                        v-if="false"
                        prop="id"
                        label="id"
                        width="100">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="职位"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="createDate"
                        label="创建时间"
                        width="150">
                </el-table-column>
                <el-table-column
                        label="是否启用"
                        width="150">
                    <template scope="scope">
                        <el-tag v-if="scope.row.enabled" type="success">已启用</el-tag>
                        <el-tag v-else type="danger">未启用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                        width="200"
                        label="操作"
                >
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="showPosition(scope.$index, scope.row)">编辑
                        </el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="deletePosition(scope.$index, scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-button size="small"
                       type="danger"
                       :disabled="this.multipleSelection.length===0"
                       style="margin-top: 15px"
                       @click="deletePostions">批量删除
            </el-button>
        </div>
        <!--编辑弹出框-->
        <el-dialog
                title="编辑职位"
                :visible.sync="dialogVisible"
                width="30%">
            <table>
                <tr>
                    <td>
                        <el-tag>职位名称</el-tag>
                    </td>
                    <td>
                        <el-input
                                class="editPosition"
                                size="small"
                                v-model="updatePos.name"
                        ></el-input>
                    </td>
                </tr>
                <tr>
                    <td>
                        <el-tag>是否启用</el-tag>
                    </td>
                    <td>
                        <el-switch
                                style="display: block;margin-left: 10px"
                                v-model="updatePos.enabled"
                                active-color="#13ce66"
                                inactive-color="#ff4949"
                                active-text="已启用"
                                inactive-text="未启用"
                                active-value="true"
                                inactive-value="false">
                        </el-switch>
                    </td>
                </tr>
            </table>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false" size="small">取 消</el-button>
                <el-button type="primary" @click="updatePosition" size="small">确 定</el-button>
             </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "PositionManager",
        data() {
            return {
                //添加职位的信息，前台只输入名字，其他数据由后台生成
                pos: {
                    name: '',

                },
                //表格数据
                Positions: [],
                dialogVisible: false,
                updatePos: {
                    id: '',
                    name: '',
                    enabled: false,
                },
                //多选框选中的数据
                multipleSelection: [],

            }
        },
        mounted() {
            this.initPositions();
        },
        methods: {
            /**
             * 表格索引
             *
             */
            indexMethod(index) {
                return index + 1;
            },
            /**
             * 初始化表单数据
             *
             */
            initPositions() {
                this.getRequest('/system/basic/pos/').then(res => {
                    if (res) {
                        this.Positions = res;
                    }
                })
            },
            /**
             * 添加职位
             *
             */
            addPosition() {
                if (this.pos.name) {
                    this.postRequest('/system/basic/pos/addPosition', this.pos).then(res => {
                        if (res) {
                            this.pos.name = '';
                            this.initPositions();
                        }
                    })
                } else {
                    this.$message.error("请输入职位名")
                }
            },

            /**
             * 打开编辑职位对话框
             *
             */
            showPosition(index, data) {
                this.dialogVisible = true;
                this.updatePos.id = data.id;
                this.updatePos.name = data.name;
                this.updatePos.enabled=data.enabled;
            },

            /**
             * 更新职位
             *
             */
            updatePosition() {
                if (this.updatePos.name) {
                    this.putRequest('/system/basic/pos/updatePosition', this.updatePos).then(res => {
                        if (res) {
                            this.dialogVisible = false;
                            this.initPositions();
                        }
                    })
                } else {
                    this.$message.error("请输入职位名称")
                }
            },

            /**
             * 删除职位
             */
            deletePosition(index, data) {
                this.$confirm('此操作将永久删除【' + data.name + '】职位, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest('/system/basic/pos/deletePosition/' + data.id).then(res => {
                        if (res) {
                            this.initPositions();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },

            /**
             * 批量删除职位
             *
             */
            deletePostions() {
                this.$confirm('此操作将永久删除' + this.multipleSelection.length + '条数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let ids = '?';
                    this.multipleSelection.forEach(item => {
                        ids += 'ids=' + item.id + '&';
                    });
                    this.deleteRequest('/system/basic/pos/deletePositions/' + ids).then(res => {
                        if (res) {
                            this.initPositions();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

            /**
             * 处理多选框
             *
             */
            handleSelectionChange(val) {
                this.multipleSelection = val;
            }
        }
    }
</script>

<style>
    .addPosInput {
        width: 300px;
        margin-right: 10px;
    }

    .PositionsTable {
        margin-top: 15px;
    }

    .editPosition {
        width: 70%;
        margin-left: 15px;
    }
</style>
